package hlhj.fhp.supreme.adapters

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.CityBean

/**
 * Created by Administrator on 2018\4\3 0003.
 */
class CityAdp(private val datas:ArrayList<CityBean.DataBean>,private val context:Context) :BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView=convertView
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.city_item,null)
        }
        val name = convertView?.findViewOften<TextView>(R.id.itemName)

        name?.text=datas[position].name

        return convertView!!
    }

    override fun getItem(position: Int): Any {
            return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return datas.size
    }

    fun <T:View> View.findViewOften(viewId:Int):T{
        var viewHodler:SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag=viewHodler
        var childView:View?=viewHodler.get(viewId)
        if (null==childView){
            childView=findViewById(viewId)
            viewHodler.put(viewId,childView)
        }
        return childView as T
    }

    /*fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }*/
}